package com.kx.yooli.kx.wechat.utils.excel;

import org.apache.commons.compress.utils.Lists;
import org.apache.poi.ss.formula.functions.T;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

/**
 * @author john_sqwen
 * @date 2024/3/20
 * @PROJECT_NAME wechat
 * @description create PoiExcelUtils by john_sqwen
 * and poiUtils value is
 */
public class PoiExcelUtils {
    private final Logger log = LoggerFactory.getLogger(PoiExcelUtils.class);

    /**
     * 写入excel 文件方法 并确保解决数据量大的时候不出现 oom问题
     * @param list data
     * @param sheetName excel sheetName
     * @throws IOException exception
     */
    public void writeDiskExcel(List<T> list,String sheetName) throws IOException {
        if(list.isEmpty() ){
            throw new RuntimeException("list is null ");
        }
        log.info("excel write method in coming");
        Sheet sheet;
        try (SXSSFWorkbook workbook = new SXSSFWorkbook(1000)) {
            sheet = workbook.createSheet("Data");
          for (int i = 0; i < list.size(); i++) {
            Row row = sheet.createRow(i);
            Cell cell = row.createCell(0);
            cell.setCellValue("Row "+i);
            //当数据到达一定程序写入磁盘
            if(i % 1000 ==0){
                ((SXSSFSheet) sheet).flushRows();
            }
        }
          //set excel compress zip or gzip
          workbook.setCompressTempFiles(true);
     }
    }

    public static void main(String[] args) {


    }
}
